
class_name CRankPlayerInfo

extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal RankType_changed(old_value, new_value) 
signal RankIndex_changed(old_value, new_value) 
signal CharID_changed(old_value, new_value) 
signal CharName_changed(old_value, new_value) 
signal CurClassID_changed(old_value, new_value) 
signal WeaponID_changed(old_value, new_value) 
signal ItemID_changed(old_value, new_value) 


var m_tblAlias
var bInited = false
func _init(_uuid)->void:
    self.UUID = _uuid
    self.RankType = 0
    self.RankIndex = 0
    self.CharID = 0
    self.CharName = ''
    self.CurClassID = 0
    self.WeaponID = 0

    self.ItemID = []
    self.ItemID.resize(5)
    for i in range(5):
        self.ItemID[i] = 0

    self.m_tblAlias = {
 
    }

func UnPack(_bufferStream):
    self.UUID = _bufferStream.ReadInt64()
    self.RankType = _bufferStream.ReadInt32()
    self.RankIndex = _bufferStream.ReadInt32()
    self.CharID = _bufferStream.ReadInt64()
    self.CharName = _bufferStream.ReadString(TableCommonDefine.ECharLen.eCL_CharName)
    self.CurClassID = _bufferStream.ReadUint8()
    self.WeaponID = _bufferStream.ReadInt32()
    for i in range(TableCommonDefine.ECharLen.eCL_ItemID):
        self.ItemID[i-1] = _bufferStream.ReadInt32()

    self.bInited = true


func GetTableName():
    return 'RankPlayerInfo'

func CopyFrom(other):
    self.RankType = other.RankType
    self.RankIndex = other.RankIndex
    self.CharID = other.CharID
    self.CharName = other.CharName
    self.CurClassID = other.CurClassID
    self.WeaponID = other.WeaponID
    for i in range(5):
        self.ItemID[i] = other.ItemID[i]


func OnDataChanged( fieldName, oldValue, newValue):
    if oldValue == newValue:
        return
    
    self.DispatchEvent(fieldName, [self, oldValue, newValue])
    #  Alias
    if self.m_tblAlias[fieldName] != null:
        for i in range(self.m_tblAlias[fieldName].size()):
            self.DispatchEvent(self.m_tblAlias[fieldName][i], [self, oldValue, newValue])

func UnPackField(_fieldId, _bufferStream, _varLen ):
    if _fieldId < 0 or _fieldId == 0xffff:
        var old = CRankPlayerInfo.new(self.m_UUID)
        old.CopyFrom( self )
        self.UnPack( _bufferStream )
        self.DispatchEvent("CRankPlayerInfo", [self, old, self])
        return true

    print("UnPackField error tblName = RankPlayerInfo, className = CRankPlayerInfo, _fieldId = ", _fieldId)
    # if _fieldId > Fields_RankPlayerInfoID.size():
    #     var szLog = 'CRankPlayerInfo.UnPackField  _fieldId(%d) < 0 or _fieldId(%d) > #Fields_RankPlayerInfoID(%d)' % [_fieldId, _fieldId,  Fields_RankPlayerInfoID.size()]
    #     print(szLog)
    #     return false

    # Fields_RankPlayerInfoID[_fieldId].reader( self, _bufferStream, _varLen)

    return true

func AddField(_fieldId, _bufferStream):
    print( "[TABLEDATA] error AddField", "CRankPlayerInfo", _fieldId )

    
func DelField(_fieldId ):
    print( "[TABLEDATA] error DelField", "CRankPlayerInfo", _fieldId )

var UUID:
    set(value):
        var old = self.m_UUID
        self.UUID = _value
        self.UUID_changed.emit(old, _value)

    get:
        return self.UUID


var RankType:
    set(value):
        var old = self.m_RankType
        self.RankType = _value
        self.RankType_changed.emit(old, _value)

    get:
        return self.RankType


var RankIndex:
    set(value):
        var old = self.m_RankIndex
        self.RankIndex = _value
        self.RankIndex_changed.emit(old, _value)

    get:
        return self.RankIndex


var CharID:
    set(value):
        var old = self.m_CharID
        self.CharID = _value
        self.CharID_changed.emit(old, _value)

    get:
        return self.CharID


var CharName:
    set(value):
        var old = self.m_CharName
        self.CharName = _value
        self.CharName_changed.emit(old, _value)

    get:
        return self.CharName


var CurClassID:
    set(value):
        var old = self.m_CurClassID
        self.CurClassID = _value
        self.CurClassID_changed.emit(old, _value)

    get:
        return self.CurClassID


var WeaponID:
    set(value):
        var old = self.m_WeaponID
        self.WeaponID = _value
        self.WeaponID_changed.emit(old, _value)

    get:
        return self.WeaponID


var ItemID:
    set(value):
        var old = self.m_ItemID
        self.ItemID = _value
        self.ItemID_changed.emit(old, _value)

    get:
        return self.ItemID


func GetItemIDByIndex(_nIndex):
    return self.m_ItemID[_nIndex]

func SetItemIDByIndex(_value,_nIndex):
    var old = self.m_ItemID[_nIndex]    
    self.m_ItemID[_nIndex] = _value
    # self.OnDataChanged( "ItemID", _nIndex, [old, _value])
    # self.OnDataChanged( "ItemID", old, _value, _nIndex )    
    return 7


